﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using WpfApp1.Dto;

namespace WpfApp1.Models
{
    public class CategoryViewModel : INotifyPropertyChanged
    {
        public CategoryViewModel(CategoryDto dto)
        {
            categoryName = dto.Name;
            Id = dto.Id;
            ParentId = dto.ParentId;
            childCagegorys = new ObservableCollection<CategoryViewModel>();
            dto.ChildCategorys.ForEach(e => childCagegorys.Add(new CategoryViewModel(e)));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 属性刷新
        /// </summary>
        /// <param name="name">发生改变的属性名称</param>
        public virtual void RefrshProperty([CallerMemberName] string name = "")
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        public string Id { get; set; }

        public string ParentId { get; set; }

        private string categoryName;

        public string CategoryName
        {
            get { return categoryName; }
            set
            {
                categoryName = value;
                RefrshProperty();
            }
        }

        private ObservableCollection<CategoryViewModel> childCagegorys;
        public ObservableCollection<CategoryViewModel> ChildCagegorys
        {
            get { return childCagegorys; }
            set
            {
                childCagegorys = value;
                RefrshProperty();
            }
        }

    }
}
